<?php
// +----------------------------------------------------------------------
// | 商品品牌业务逻辑
// +----------------------------------------------------------------------
// | Author: myh
// +----------------------------------------------------------------------
namespace app\admin\service;

use app\common\model\GoodsBrandModel;
use app\common\model\GoodsCategoryModel;

class GoodsBrandService
{
    /**
     * 新增
     * @param  array  $data  新增的数据
     * @return int
     */
    public static function save($data){
        $model = new GoodsBrandModel;
        $model->save($data);
        return success($model->id);
    }

    /**
     * 更新
     * @param  array  $data 更新的数据
     * @return int
     */
    public static function update($data){
        $model = GoodsBrandModel::find($data['id']);
        if(!$model){
            return failure(config('error.er15')['code'],config('error.er15')['msg']);
        }
        return success($model->save($data));
    }

    /**
     * 删除
     * @param  string  $ids  需要删除数据的id
     */
    public static function destroy($ids){
        if(!empty($ids)){
            GoodsBrandModel::deleteById($ids);
        }
        return success();
    }

    /**
     * 分页
     * @return array
     */
    public static function page($param){
        $list = GoodsBrandModel::page($param, ['order'=>'sort,desc']);
        if ($list['total'] > 0) {
            $cateIds = array_column($list['list'], 'cate_id');
            $categoryList = GoodsCategoryModel::where('id', 'in', $cateIds)->field('id,category_name')->select()->toArray();

            foreach ($list['list'] as $k => $v) {
                $list['list'][$k]['category_name'] = '';
                foreach ($categoryList as $vv) {
                    if ($v['cate_id'] == $vv['id']) {
                        $list['list'][$k]['category_name'] = $vv['category_name'];
                    }
                }
            }
        }

        return success($list);
    }

}